using Microsoft.NetEnterpriseServers;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.UI;
using Microsoft.SqlServer.Management.UI.VSIntegration;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
	internal class ScriptOptionsWizardPage : WizardPage
	{
		private PropertyGrid idScriptOptions;

		private Label idLabelOptions;

		private GenScriptWizForm parent;

		public ScriptOptionsWizardPage(WizardForm wizard) : base(wizard)
		{
			this.parent = (GenScriptWizForm)wizard;
			this.InitializeComponent();
			this.LoadStrings();
			base.PageID = 22;
			this.SubTitle = GenScriptWizSR.GenScriptGeneralFormSubtitle;
			this.Title = GenScriptWizSR.GenScriptGeneralFormTitle;
			base.HelpTopicKeyword = "sql9.swb.generatescriptswizard.choosescriptoptions.f1";
		}

		private void LoadStrings()
		{
			this.idLabelOptions.Text = GenScriptWizSR.OptionsLabel;
		}

		protected override void OnEnterPage(EventArgs e)
		{
			GenWizOptionsBase genWizOptionsBase = (GenWizOptionsBase)base.WizardInputs.FindItem(1);
			IScriptingOptions shellScriptingOptions = ((GenScriptWizForm)this.Wizard).ShellScriptingOptions;
			if (shellScriptingOptions != null)
			{
				genWizOptionsBase.LoadShellScriptingOptions(shellScriptingOptions);
			}
			this.idScriptOptions.SelectedObject = genWizOptionsBase;
			this.idScriptOptions.Focus();
			GenWizGeneralData genWizGeneralData = (GenWizGeneralData)base.WizardInputs.FindItem(0);
			if (!base.WizardInputs.FindBool(3))
			{
				this.AddItemsFromGenWizObjects(genWizGeneralData);
			}
			if (genWizGeneralData.SaveScriptDestination == GenWizGeneralData.ScriptDestination.ToProvider)
			{
				SqlServerVersion targetVersion;
				if (genWizOptionsBase.ScriptCompatibilityOption == GenWizOptionsBase.ScriptCompatabilityOptions.Script80Compat)
				{
					targetVersion = SqlServerVersion.Version80;
				}
				else if (genWizOptionsBase.ScriptCompatibilityOption == GenWizOptionsBase.ScriptCompatabilityOptions.Script90Compat)
				{
					targetVersion = SqlServerVersion.Version90;
				}
				else
				{
					targetVersion = SqlServerVersion.Version100;
				}
				try
				{
					genWizGeneralData.ScriptingDriver.CheckPublishTransaction(targetVersion);
					((GenWizPublishOptions)genWizOptionsBase).UseTransactionForPublish = GenWizOptionsBase.BooleanTypeOptions.True;
				}
				catch (InvalidOperationException)
				{
					((GenWizPublishOptions)genWizOptionsBase).UseTransactionForPublish = GenWizOptionsBase.BooleanTypeOptions.False;
				}
			}
		}

		private void AddItemsFromGenWizObjects(GenWizGeneralData generalData)
		{
			generalData.ScriptingDriver.Urns.Clear();
			List<GenWizObjectType> list = (List<GenWizObjectType>)base.WizardInputs.FindItem(2);
			UrnCollection urnCollection = null;
			UrnCollection urnCollection2 = null;
			foreach (GenWizObjectType current in list)
			{
				if (current.IsSelected)
				{
					if (current.PageId == GenScriptWizForm.ScriptWizardPage.DatabaseRoles)
					{
						urnCollection = current.SelectedUrns;
					}
					else
					{
						if (current.PageId == GenScriptWizForm.ScriptWizardPage.Tables)
						{
							urnCollection2 = current.SelectedUrns;
							using (IEnumerator<Urn> enumerator2 = urnCollection2.GetEnumerator())
							{
								while (enumerator2.MoveNext())
								{
									Urn current2 = enumerator2.Current;
									generalData.ScriptingDriver.Urns.Add(current2);
								}
								continue;
							}
						}
						foreach (Urn current3 in current.SelectedUrns)
						{
							generalData.ScriptingDriver.Urns.Add(current3);
						}
					}
				}
			}
			if (urnCollection == null)
			{
				urnCollection = new UrnCollection();
			}
			if (urnCollection2 != null)
			{
				generalData.ScriptingDriver.AddFullTextCatalog(generalData.ScriptingDriver.ServerObject.Databases[generalData.DatabaseName], urnCollection2, urnCollection);
			}
			if (urnCollection != null)
			{
				generalData.ScriptingDriver.AddDatabaseRoles(generalData.ScriptingDriver.ServerObject.Databases[generalData.DatabaseName], urnCollection, true);
			}
		}

		private void idScriptOptions_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
		{
			if (e.ChangedItem.Label == GenScriptWizSR.ScriptStatistics && (GenWizOptionsBase.ScriptStatisticsOptions)e.ChangedItem.Value == GenWizOptionsBase.ScriptStatisticsOptions.ScriptStatsAll)
			{
				this.parent.MessageProvider.ShowMessage(GenScriptWizSR.GenerateScriptStatsWarning, GenScriptWizSR.WizardText(""), ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this);
			}
		}

		private void InitializeComponent()
		{
			this.idScriptOptions = new PropertyGrid();
			this.idLabelOptions = new Label();
			base.SuspendLayout();
			this.idScriptOptions.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.idScriptOptions.Location = new Point(16, 36);
			this.idScriptOptions.Name = "idScriptOptions";
			this.idScriptOptions.Size = new Size(432, 197);
			this.idScriptOptions.TabIndex = 1;
			this.idScriptOptions.PropertyValueChanged += new PropertyValueChangedEventHandler(this.idScriptOptions_PropertyValueChanged);
			this.idLabelOptions.Location = new Point(16, 12);
			this.idLabelOptions.Name = "idLabelOptions";
			this.idLabelOptions.Size = new Size(427, 16);
			this.idLabelOptions.TabIndex = 0;
			this.idLabelOptions.Text = GenScriptWizSR.OptionsLabel;
			base.Controls.Add(this.idLabelOptions);
			base.Controls.Add(this.idScriptOptions);
			base.Name = "ScriptOptionsWizardPage";
			base.Size = new Size(464, 245);
			base.ResumeLayout(false);
		}
	}
}
